<?php


class incident_create
{
	var $data= Array();
	var $valid = false;

	function incident_create()
	{
		global $lang, $db, $properties, $style;

                $style->addJavascript('js/ajax.js');
                $style->addJavascript('js/incidentEditDriver.js');

		$this->data['event'] = $_GET['event'];
		$this->data['division'] = $_GET['division'];
		$this->data['race'] = $_GET['race'];

                
                $userfilter = '';
                if(! (isset($_SESSION['valid_admin']) && $_SESSION['valid_admin'] > 2))
                    $userfilter = ' and DATE_ADD(ed_resultsPosted, INTERVAL ' . $properties['event_incident_report'] .
                                ' HOUR) > NOW() and r_driver = ' . $_SESSION['valid_id'];

		$q = 'SELECT e_title, d_title, c_title, r_racelaps, t_fullTitle
			  FROM ' . EVENTS_TABLE . ', ' . EVENTDATES_TABLE . ', ' . DIVISIONS_TABLE . ', ' . RACERESULTS_TABLE . ', ' . CHAMPIONSHIPS_TABLE . 
			  	   ', ' . EVENTSIGNUPS_TABLE . ', ' . TRACKS_TABLE . '
			  WHERE e_id = ed_event and e_id = r_event and e_championship = c_id and d_id = ed_division and d_champid = c_id and c_incidents = 1
      				and r_driver = es_driver and es_event = e_id and es_division = d_id and e_track = t_id and e_id = ' .
					$this->data['event'] . ' and d_id = ' . $this->data['division'] . ' and r_race = ' . $this->data['race'] . 
					$userfilter;

		$res = $db->sql_query($q);
		if ($row = $db->sql_fetchrow($res))
		{
			$this->data['championshipName'] = $row['d_title'] == $row['c_title'] ? $row['c_title'] : $row['c_title'] . ': ' . $row['d_title'];
			$this->data['eventName'] = $row['e_title'] . ' ' . $row['t_fullTitle'] . ' ' . $lang['ResultsRace'] . ' ' . $this->data['race'];
			$this->data['lapOptions'] = '';
			
			for($x = 0; $x <= $row['r_racelaps']; $x++)
			{
				$this->data['lapOptions'] .=  '<option value="' . $x . '">' . $x . '</option>';
			}
			
			$q = 'SELECT pf_drivername, user_id 
				  FROM ' . DRIVERNAME_TABLE . ', ' . RACERESULTS_TABLE . ', ' . EVENTSIGNUPS_TABLE . '
				  WHERE r_driver = user_id and es_driver = user_id and r_event = es_event and r_race = ' . $this->data['race'] . ' 
				  		and r_event = ' . $this->data['event'] . ' and es_division = ' . $this->data['division'] . '
				  ORDER BY pf_drivername';
			$res = $db->sql_query($q);
			$this->data['driverOptions'] = '';
		 	while ($row = $db->sql_fetchrow($res))
			{
			 	$this->data['driverOptions'] .=  '<option value="' . $row['user_id'] . '">' . $row['pf_drivername'] . '</option>';
			}
			
			$this->data['driverRolesOptions'] = '<option value="0">' . $lang['IncidentAtFault'] . '</option><option value="1">'. 
												$lang['IncidentVictim'] . '</option>';
			if(isset($_SESSION['incident']))
			{
				unset($_SESSION['incident']);
			}
			$this->valid = true;
		}
	}
	
	function render()
	{
		include_once('includes/addMessage.php');
		global $lang, $skin_dir;
		
		if($this->valid)
		{	
			$data = $this->data;
			
			$mess = new addMessage($lang['InicidentAddExplanation'], $lang['IncidentAdd']);
			$mess->render();
			
			include $skin_dir . 'tpl/incidentCreate.tpl';	
		}
		else
		{
		
			$mess = new addMessage($lang['InicidentAddError'], $lang['IncidentAdd']);
			$mess->render();
		}
	}
}

?>